import java.util.Random;

public class Text {
    
    //先定义两个静态成员变量
    static int sum1 = 0;
    static int sum2;
    public static void main(String[] args) throws InterruptedException {
        //编写代码实现多线程数组求和
        int[] arr = new int[10000000];
        //求开始的时间
        long startTime = System.currentTimeMillis();
        Random r = new Random();
        //生成1到100的随机数
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (r.nextInt(100)+1);
        }

        Thread t1 = new  Thread(() -> {
            //计算偶数下标的和
            for (int i = 0; i < arr.length; i+=2) {
                sum1 = sum1 + arr[i];

            }
            System.out.println("线程1:" + sum1);
        });

        //启动线程1
        t1.start();
        Thread t2 = new Thread(()->{
            //计算奇数下标的 和
            for (int i = 1;i<arr.length;i+=2) {
                sum2 = sum2 + arr[i];
            }
            System.out.println("线程2:" + sum2);
        });

        //启动线程
        t2.start();

        //等待t1 和 t2 线程结束
        t1.join();
        t2.join();
        long endTime = System.currentTimeMillis();
        System.out.println("结果:" + (sum1 + sum2));
        System.out.println("执行总时间:" + (endTime - startTime));
    }
}
